#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>
#include "fonction.h"



void load_hamma(PERSO *perso,char buf[] )
{
int i;
for (i = 0; i < 3; i++) 
       {
		sprintf(buf, "hammaright%d.bmp", i);
		perso->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "hammaleft%d.bmp", i);
		perso->img_left[i] = load_bitmap(buf, NULL);
	}	
}
void load_abu(PERSO *perso1,char buf[])
{
int i ;
for (i = 0; i < 3; i++) 
{
		sprintf(buf, "abouiyadhright%d.bmp", i);
		perso1->img_right[i] = load_bitmap(buf, NULL);
		sprintf(buf, "abouiyadhleft%d.bmp", i);
		perso1->img_left[i] = load_bitmap(buf, NULL);
}		
}


void load_flip (weapon *fl1, weapon *fl)
	{
        fl->img_flip=load_bitmap("flip.bmp",NULL);
	fl1->img_flip=load_bitmap("flip.bmp",NULL);
        }
void init_hamma (PERSO *perso)
{
       
	perso->direction = 0;
	perso->x = (SCREEN_W / 2)+400;
	perso->y = (SCREEN_H / 2)+160;
	perso->depl=1;
	perso->vie=5 ;

}  
  
void init_abu (PERSO *perso1)
{
        
	perso1->direction = 0;
	perso1->x = (SCREEN_W / 2)-380;
	perso1->y = (SCREEN_H / 2)+160;
	perso1->depl=1;
	perso1->vie=5;
}
void move_hamma_up(PERSO *perso,int *compteur1)
 {
if (key[KEY_UP] && (perso->x < SCREEN_W - perso->img_right[0]->w)) 
        {
			*compteur1++;

			(perso->y)-=5;
	}
}	
void move_hamma_right(PERSO *perso,int *compteur1)
{
if (key[KEY_RIGHT] && (perso->x < SCREEN_W - perso->img_right[0]->w)) 
                {
			*compteur1++;

			(perso->x)+=perso->depl;
			perso->direction = 0;
		}
}		
void move_hama_left(PERSO *perso,int *compteur1)
{
if (key[KEY_LEFT] && (perso->x > 0 + perso->img_left[0]->w)) {
			*compteur1++;

			(perso->x)-=perso->depl;
			perso->direction = 1;
		}
}
void move_perso(PERSO *perso,int *compteur1,PERSO *perso1,int *compteur2)
{
                move_hamma_up(perso,compteur1);
                move_hama_left(perso,compteur1);
		move_hamma_right(perso,compteur1);
		 move_abu_droite(perso1,compteur2)  ;        
		move_abu_gauche(perso1,compteur2); 
		jump_abu(perso1,compteur2);
}

void move_abu_droite(PERSO *perso1,int *compteur2)
{
		if (key[KEY_D] && (perso1->x < SCREEN_W - perso1->img_right[0]->w)) 
		{
			*compteur2++;

			(perso1->x)+=perso1->depl;
			perso1->direction = 0;
		}
				
}	
void move_abu_gauche(PERSO *perso1,int *compteur2)
{		
if (key[KEY_A] && (perso1->x > 0 + perso1->img_left[0]->w)) 
	{
		        *compteur2++;

			(perso1->x)-=perso1->depl;
			perso1->direction = 1;
	}
}
void jump_abu(PERSO *perso1,int *compteur2)
	{
		if (key[KEY_W] && (perso1->x < SCREEN_W - perso1->img_right[0]->w)) 
		{
			*compteur2++;

			(perso1->y)-=5;
		}
	}

void tir_abu(PERSO *perso1,weapon *fl1,BITMAP *Buffer)
	{
		if (key[KEY_F]) 
		{
		fl1->x=perso1->x ;
		fl1->y=perso1->y ;		
		
	}

if (perso1->direction==1)
{
fl1->x-=5 ;
}		
   if (perso1->direction==0)
{
fl1->x+=5 ;
}
draw_sprite(Buffer, fl1->img_flip,fl1->x, fl1->y);
			
	}

void tir_hamma (weapon *fl,PERSO *perso,BITMAP *Buffer)
     {
		if (key[KEY_L]) 
		{
		fl->x=perso->x ;
		fl->y=perso->y ;		
		
	}

if (perso->direction==1)
{
fl->x-=5 ;
}		
   if (perso->direction==0)
{
fl->x+=5 ;
}
draw_sprite(Buffer, fl->img_flip,fl->x, fl->y);
			
	}	

void draw_hamma(BITMAP *Buffer,weapon fl,PERSO perso,int m,int compteur1)	 
{     
 	
		if (perso.direction == 0)
			draw_sprite(Buffer, perso.img_right[compteur1 % NB_IMAGES ],perso.x, perso.y);
		else
			draw_sprite(Buffer, perso.img_left[compteur1 % NB_IMAGES ],perso.x, perso.y);

		
	
	if (compteur1 == 10000)
			compteur1 = 0;
		
}
	
	
     
void draw_abu(BITMAP *Buffer,weapon fl1,PERSO perso1,int f,int compteur2)
	{	
       		if (perso1.direction == 0)
			draw_sprite(Buffer, perso1.img_right[compteur2 % NB_IMAGES ],perso1.x, perso1.y);
		else
			draw_sprite(Buffer, perso1.img_left[compteur2 % NB_IMAGES ],perso1.x, perso1.y);
		
	if (compteur2 == 10000)
{		
compteur2 = 0;
}
}
void gravity(int *y)
{
 *y+=4 ;
}
void gravitee(PERSO *perso,PERSO *perso1)
{
 if (perso->y<470)
{
gravity(&perso->y) ;
}
if ((perso1->y)<470)
{
gravity(&perso1->y) ;
}
}
int collision_gauche(weapon fl1)
{
int x,y;
x=fl1.x ;
y=fl1.y+ (fl1.img_flip->h) /2 ;
if (get_pixel(screen,x,y)==170)
{
return 1 ;
}
return 0 ;
}
/*int collision_droite(weapon fl)
{
int x,y ;
x=fl.x + fl.img_flip->w ;
y=fl.y + (fl.img_flip->h) /2+15 ;
if (get_pixel(screen,x,y)==170)
return 1 ;

return  0 ;
}*/

int bounding_box_collision(int b1_x, int b1_y, int b1_w, int b1_h, int b2_x, int b2_y, int b2_w, int b2_h)
{
    if ((b1_x > b2_x + b2_w - 1) || // is b1 on the right side of b2?
        (b1_y > b2_y + b2_h - 1) || // is b1 under b2?
        (b2_x > b1_x + b1_w - 1) || // is b2 on the right side of b1?
        (b2_y > b1_y + b1_h - 1))   // is b2 under b1?
    {
        // no collision
        return 0;
    }
 
    // collision
    return 1;
}

int touch_abu (PERSO *perso1,weapon *fl)
{

if (bounding_box_collision(fl->x, fl->y, 70, 80, perso1->x, perso1->y, 100, 150)==1)
{
perso1->vie --;
printf("%d \n",perso1->vie);
fl->x=1300;
fl->y=1300;
return 1;
}
return 0;
}

int get_pixel(BITMAP* Buffer,int x,int y)
{
	int tmp;
	tmp=getpixel(Buffer,x,y);
	return((getr(tmp) + getb(tmp) + getg(tmp)) / 3);
}

/*SAMPLE *sound;
if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL)) {
allegro_message("Sound Error: %s", allegro_error);
return 1; }

set_volume(255, 255); //digital and music to max levels

sound = load_sample("boxing_bell.wav");

play_sample(sound, 255, 128, 1000, 0);

}

/*void soundm(sound)
{

if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL))
{
allegro_message("Sound Error: %s", allegro_error);
}
set_volume(100, 100);
sound = load_sample("/home/jihen/Bureau/buzzer_x.wav");

play_sample(sound, 255, 128, 1000, 0);
if (!sound)
{
allegro_message("Error reading wav file");

}
}*/


